<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Admin\Model;

use Think\Model;

class PushOrderModel extends Model {

        protected $_validate=array(
                  array("user_id","require","用户信息为空，请刷新后重试",1,"regex",1),
                  array("package_id","require","请选择套餐",1,"regex",1),
                  array("push_content","require","请填写推送文案",1,"regex",1),
                  array("money","currency","价格不正确",1,"regex")
                );

        //根据id查询代写服务信息
        public function getPushList($where=""){

            $join=array('cloud_user ON cloud_push_order.user_id = cloud_user.id','cloud_push_package ON cloud_push_order.package_id = cloud_push_package.id');
            $field = "cloud_push_order.*,cloud_user.username as userName,cloud_push_package.title as packageName";
            $res=getPages($this,$where,"","",$join,$field);
            
            return $res;
        }
         //根据id查询代写服务信息
        public function getPushById($id){

            $join=array('cloud_user ON cloud_push_order.user_id = cloud_user.id','cloud_push_package ON cloud_push_order.package_id = cloud_push_package.id');
            $field = "cloud_push_order.*,cloud_user.username as userName,cloud_push_package.title as packageName";
            
            $res=$this->join($join)->field($field)->where("cloud_push_order.id=%s",$id)->find();

            //格式化内容
            //$res["content"]=htmlspecialchars_decode($res["content"]);

            return $res;
        }

        //审核代写服务信息
        public function changeData($map){

            if(!$map){
                return false;
            }

            //拆分id
            $ids=explode(",",$map["ids"]);
            
            if($map["type"]=="apply"){
                $data["is_apply"]=$map["status"];
            }else{
                $data["is_ok"]=$map["status"];
            }

            $res=$this->where(array("id"=>array("in",$ids)))->save($data);
            
            if($res > 0){                
                return true;
            }else{
                return false;
            }

        }

        //上传反馈文件
        public function upPushFeedback($files,$map){

            if(!$map["id"] || !$files){

                $this->error="数据不能为空，上传失败";
                return false;
            }
            //上传图片
            $push_url=uploadFile($files,"pushFeedback");

            if($push_url["code"]==100){
                $this->error=$push_url["mess"];
                return false;
            }else{

                $data["id"]=$map["id"];
                $data["push_file"]=$push_url["mess"];

                $res=$this->save($data);
                if($res===false){
                    $this->error="上传失败";
                    return false;
                }
                return true;
            }

        }


        //添加、修改云推送
        public function editData($data,$files){


            if(!$data){
                $this->error="数据不能为空";
                return false;
            }
             //上传图片
            $push_img=uploadPhotos($files,"push");
            
            if($push_img["code"]!="200"){
                $this->error=$push_img["mess"];
                return false;
            }

            $data["user_id"]=$_SESSION["user"]["id"];
            $data["package_id"]=$data["pushPackage"];
            $data["push_content"]=$data["content"];

            if(!$this->create($data)){

                $this->error=$this->getError();
                return false;
            }

            $this->startTrans();

            if(empty($data["id"])){

                $this->push_img=$push_img["mess"];
                $this->add_time=date("Y-m-d H:i:s");

                $res=$this->add();

                if($res===false){
                    $this->rollback();
                    $this->error="添加失败，请刷新重试";
                    return false;
                }

                $this->commit();
                return true;

            }else{

                $this->rollback();
                return false;
            }

        }
    
   }


?>
